package com.wen.yygh.hosp.controller.api;


import com.wen.yygh.hosp.service.ScheduleService;
import com.wen.yygh.model.hosp.Schedule;
import com.wen.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import com.wen.yygh.common.helper.HttpRequestHelper;
import com.wen.yygh.common.result.Result;
import com.wen.yygh.hosp.service.DepartmentService;
import com.wen.yygh.hosp.service.HospitalService;
import com.wen.yygh.hosp.service.HospitalSetService;
import com.wen.yygh.model.hosp.Department;
import com.wen.yygh.model.hosp.Hospital;
import com.wen.yygh.vo.hosp.DepartmentQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.PortUnreachableException;
import java.sql.ResultSet;
import java.util.Map;

/**
 * @author Wenjl
 */
@Slf4j
@RestController
@Api("医院上传")
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    /**
     * 获取医院信息
     *
     * @param request
     * @return
     */
    @ApiOperation("获取医院信息")
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院id
        String hoscode = (String) paramMap.get("hoscode");
        //查询数据库中的密钥
        String hospitalSign = hospitalSetService.getSingKey(hoscode);

        HttpRequestHelper.ifSingKey(paramMap, hospitalSign);
        Hospital hospital = hospitalService.getHospitalByHoscode(hoscode);
        return Result.ok(hospital);
    }

    /**
     * 添加医院信息
     *
     * @param request
     * @return
     */
    @ApiOperation("添加医院信息")
    @PostMapping("saveHospital")
    public Result save(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        String hoscode = (String) paramMap.get("hoscode");
        //查询数据库中的密钥
        String hospitalSign = hospitalSetService.getSingKey(hoscode);
        //调用方法比较密钥
        HttpRequestHelper.ifSingKey(paramMap, hospitalSign);
        //因为传输logdata中的+号变成了空格要给他变回来
        String logoData = (String) paramMap.get("logoData");
        String replace = logoData.replace(" ", "+");
        paramMap.put("logoData", replace);

        //调用service的方法
        hospitalService.save(paramMap);
        return Result.ok();

    }

    /**
     * 查看科室列表
     *
     * @param request
     * @return
     */
    @ApiOperation("查看科室列表")
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //查询数据库中的密钥
        String hospitalSign = hospitalSetService.getSingKey(hoscode);
        //判断密钥是否一致
        HttpRequestHelper.ifSingKey(paramMap, hospitalSign);
        //页码
        Integer pageNum = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.valueOf((String) paramMap.get("page"));
        //页面大小
        Integer pageSize = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.valueOf((String) paramMap.get("limit"));
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> departmentPage = departmentService.findPageDepartment(pageNum, pageSize, departmentQueryVo);
        return Result.ok(departmentPage);
    }

    /**
     * 添加科室信息
     *
     * @param request
     * @return
     */
    @ApiOperation("添加科室信息")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院id
        String hoscode = (String) paramMap.get("hoscode");
        //查询数据库中的密钥
        String hospitalSign = hospitalSetService.getSingKey(hoscode);

        HttpRequestHelper.ifSingKey(paramMap, hospitalSign);
        departmentService.save(paramMap);
        return Result.ok();
    }

    /**
     *删除科室信息
     */
    @ApiOperation("删除科室信息")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院id
        String hoscode = (String) paramMap.get("hoscode");
        //查询数据库中的密钥
        String hospitalSign = hospitalSetService.getSingKey(hoscode);

        HttpRequestHelper.ifSingKey(paramMap, hospitalSign);
        //获取科室id
        String depcode = (String) paramMap.get("depcode");
        departmentService.remove(hoscode, depcode);
        return Result.ok();

    }

    /**
     * 新增排版列表
     * @param request
     * @return
     */
    @ApiOperation("新增排版列表")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        String hoscode = (String) paramMap.get("hoscode");
        //查询数据库中的密钥
        String hospitalSign = hospitalSetService.getSingKey(hoscode);
        //调用方法比较密钥
        HttpRequestHelper.ifSingKey(paramMap, hospitalSign);
        scheduleService.save(paramMap);
        return Result.ok();
    }

    /**
     * 排版列表
     */
    @PostMapping("schedule/list")
    @ApiOperation("排版列表")
    public Result findScheduleList(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        String hoscode = (String) paramMap.get("hoscode");
        //查询数据库中的密钥
        String hospitalSign = hospitalSetService.getSingKey(hoscode);
        //调用方法比较密钥
        HttpRequestHelper.ifSingKey(paramMap, hospitalSign);
        //获取页吗
        Integer pageNum = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.valueOf((String) paramMap.get("page"));
        //获取页面大小
        Integer pageSize = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.valueOf((String) paramMap.get("limit"));
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();

        Page<Schedule> schedulePage = scheduleService.findSchedulePage(pageNum, pageSize, scheduleQueryVo);
        return Result.ok(schedulePage);
    }

    /**
     * 删除排班
     * @param request
     * @return
     */
    @PostMapping("schedule/remove")
    @ApiOperation("删除排班")
    public Result removeSchedule(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编码
        String hoscode = (String) paramMap.get("hoscode");
        //查询数据库中的密钥
        String hospitalSign = hospitalSetService.getSingKey(hoscode);
        //调用方法比较密钥
        HttpRequestHelper.ifSingKey(paramMap, hospitalSign);

        //排版编号
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }
}
